home *** CD-ROM | disk | FTP | other *** search
/ Commodore 64 Scene Diskmags Assortment / Commodore_CEE_Vol._1_Issue_05_1995_Jack_Vander_White_Disk_3_of_3_Side_A.d64 / sid noise < prev    next >
Text File  |  2023-02-26  |  16KB  |  319 lines

  1. Uncovering the waveforms of the SID -----------------------------------
  2.  
  3. Document version 1.0, 19 Apr 1995.
  4.  
  5. After posting to comp.sys.cbm for info on an algorithm for reproducing the output of the noise-waveform, someone followed up with an article on how to do pseudo-random generators in hardware.  The poster described a leftshifting register scheme with an exclusive-or gate as feed for the outshifted bit 0. This inspired me to examine the SID and especially the noise-waveform in more detail.
  6.  
  7. This document descibes some of the things, I discovered in my search.  I give algorithms for reproducing the pulse- and noise-waveforms. Introductory work on reproducing the waveforms corresponding to specific frequencies have been done, but rather than finish that job, I'd like to give you the info on the noise waveform first.
  8.  
  9. Sampling the waveform ---------------------
  10.  
  11. The waveforms of the SID in the c64 and c128 can be examined, because the SID provides a 8-bit output register of the waveform of voice 3 in register $1b. The exact waveform can also be examined from the "start" of the waveform, because the test-bit (bit 3 in register $12 for voice 3) can be used to reset the random-waveform.
  12.  
  13. To examine the data, we want to be able to sample the output in a consistent way.  First step is to establish the sampling-rate to be used.  Of course the sampling-rate must depend on the frequency used in registers $0e and $0f, so our job is to determine the dependency between the frequency and the waveform "wavelength" in cycles, i.e. the number of cycles between the waveform-values change.
  14.  
  15. To this end, we want to read the waveform output of register $1b as fast as possible.  With a REU it is possible to sample the value every cycle, and with the program "Cyclewise", which samples $10000 bytes of the waveform output into bank 0 of a REU, I have collected the data in table 1 which are valid for the noise waveform.  The number in the first column is the frequency put into registers $0e and $0f.  The second number is the number of cycles each value of the waveform at least lasted, while the third number is the number of cycles the first value ($fe) lasted when the sampling is done using the construct:
  16.  
  17.         sta $d412       ;Start waveform
  18.         stx $df01       ;Start sampling
  19.  
  20. The STA and STX instructions take 4 cycles, but the data in the table suggests that the sampling is delayed only 3 cycles.  Furthermore the first value count is probably 1.5 times longer than the waveform.
  21.  
  22. Table 1: Frequency, wavelength and initial delay
  23.  
  24. Frequency       Wavelength      1st value cnt
  25.   $ffff           $10.001000      $16
  26.   $C000           $15.555555      $1D
  27.   $AAAA           $18.001800      $22
  28.   $8000           $20             $2D
  29.   $6000           $2A.AAAAAB      $3D
  30.   $4000           $40             $6D
  31.   $3222           $51.B3F644      $78
  32.   $3000           $55.555555      $7D
  33.   $1000          $100            $17D
  34.   $0100         $1000           $17FD
  35.  
  36. This leads to the conclusion that the frequencies can generated with a loop like this for the noise waveform:
  37.  
  38.   void Frequency-generator(long freq) {SHIFT-+}
  39.     long delay=0x180000;        /* C-notation for $180000 */
  40.     long cycle=0;
  41.     for (;;;) {SHIFT-+}                 /* Repeat forever */
  42.       delay= delay-freq;
  43.       if (delay<0) {SHIFT-+}
  44.         delay= delay+0x100000;  /* C-notation for $100000 */
  45.         waveform output= calculate new value for waveform;
  46.       {SHIFT--};
  47.       waveform[cycle]= waveform output;
  48.       cycle= cycle+1;
  49.     {SHIFT--}
  50.   {SHIFT--}
  51.  
  52. Furthermore, we notice that the highest frequency that is 100% cycle-aligned is $8000, which gives us a wavelength of 32 cycles.  In the following dis- cussion this frequency is assumed to be used.
  53.  
  54. Notice that the above program is not garanteed to be 100% correct, since I haven't tested it.  Specifically is the value of $180000 a quick hack and it might be $17ffff just as well.
  55.  
  56.  
  57. The noise-waveform ------------------
  58.  
  59. The next step is to determine whether the noise waveform loops, since this knowledge is useful in respect to an algorithm based on manipulating an internal register.  If the algorithm is based upon manipulation an internal register (by doing for instance shifting and exclusive-or), the values  have
  60.  
  61. to restart sometime, because an internal register on n bits can only hold 2^n different values.  In other words, we will try to establish whether the output stream from $d41b will repeat itself and if it doesn't in a set period of time, say n clockcycles, we will know that the internal register will be of at least log2(n/32) bits, since the value changes each 32 clockcycles (note: log2(x) is the same as log(x)/log(2)).
  62.  
  63. The program "Loopchecker" checks for loops with an algorithm like this:
  64.  
  65.   void Loopchecker() {SHIFT-+}
  66.     start noise-waveform();
  67.  
  68.     /* We want to sample *inside* the potential loop */
  69.     wait a while();
  70.  
  71.     /* Sample 256 values */
  72.     for (i=0;i<256;i=i+1)
  73.       data[i]= peek($d41b);
  74.  
  75.     /* Sample until those 256 values arrive again */
  76.     do {SHIFT-+}
  77.       i=0;
  78.       while (peek($d41b) = data[i]) do
  79.         i=i+1;
  80.     while (i<256);
  81.   end;
  82.  
  83. This program will terminate if a sequence of 256 recorded bytes will appear again later. If the 256 bytes reappear, we can be quite certain that the waveform loops, since the chance of this happening with a totally random source is infinitely small (well below 1e-500).
  84.  
  85. Marko Makela and I hacked a loopchecker together over the IRC, and our results with a 16 cycle sampling-rate was that the computer terminated after approxi- mately 2 minutes and 15 seconds.  However, our results weren't consistent, partly because of the 16 cycle sampling frequency, which isn't completely cycle-aligned and partly because the reseting of a waveform with the TEST-bit does not reset the waveform immediately, but rather $2000-$8000 cycles later (this figure varies greatly, does anybody know of a way to reset the waveform fast?).
  86.  
  87. The "Loopchecker" program given below fixes these errors, and if you run this program, the computer will terminate after approximately 4 minutes and 32 se- conds or 272 seconds.  Firstly this means that the waveform repeats. Secondly, when the waveform changes every 32 cycles, this means that the length of the loop is approximately
  88.  
  89.   (272 sec * 980000 cyc/sec)/32 cyc/bytes {$de}= 8.0 Megabytes.
  90.  
  91. Thirdly, this implies an internal register length of log2(8.0 MB)= 23 bits. And finally, we must admit that it would be a difficult task to sample the entire 8 MB on a c64 with only 64Kb memory.  It is possible to sample the lot in chunks and saving it on multiple disks, but this is not a trivial task. Notice that the data probably can't be packed much since they are random. It should be mentioned that by changing the delayloop after the noisewaveform is selected, it can be demonstrated that the waveform indeed loops after 8 megabytes of data nomatter where in the waveform cycle you are.
  92.  
  93. With the implication of an internal register of 23 bits, the next step is to find a pattern in the data which might hint the algorithm used to produce the data.  Since we know a shifting and eor scheme can be used, it might be useful to take a look at the data in binary:
  94.  
  95. 11111110 11111100 11111100 11111100 11111000 11111000 11111000 11111000 11110000 11110000 11100000 11100000 11100000 11000000 11000000 11000000 11000000 10000001 New value for bit 0! 10000001 00000011 00000011 00000011 00000110 00000110 00000100 00000100 00001100 00001000 00011000 00011000 00011000 00110000 00110000 ...  It should be quite clear that some kind of shifting scheme is used.  Further examination of the data suggests that the internal register indeed is on 23 bits, where the mapping between the 8 bits in the output and the internal 23 bit register is like this:
  96.  
  97. Internal register bit number
  98.         22 21 20 19 18 17 16 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0 Output bit from register $1b
  99.          7     6           5        4     3           2        1     0
  100.  
  101. The first data from the output can be reproduced with this layout if the internal register is leftshifted, and the initial value of the internal register is:
  102.          1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  0  0  0
  103.  
  104. Now we need to explain the new bits appearing in bit 0 of the data.  Even further examination of the data implies that an eor-gate is used to feed bit 0 of the internal register.  I have found that the bits 22 and 17 of the inter- nal register provides the feed for bit 0 through an eor-gate, which gives us the entire mechanism:
  105.  
  106. 22<-21<-20<-19<-18<-17<-16<-15-14<-13<-12<-...<-8<-7<-6<-5<-4<-3<-2<-1<-0 {CBM--}                   {CBM--}                                                   {CBM--} +---->----->-----> eor ->----->----->----->----->----->----->----->---->+
  107.  
  108. This can also be expressed as a C-program like this:
  109.  
  110. /* Test a bit. Returns 1 if bit is set. */ long bit(long val, byte bitnr) {SHIFT-+}
  111.   return (val & (1<<bitnr))? 1:0; {SHIFT--}
  112.  
  113.  
  114. /* Generate output from noise-waveform */ void Noisewaveform {SHIFT-+}
  115.   long bit22;   /* Temp. to keep bit 22 */
  116.   long bit17;   /* Temp. to keep bit 17 */
  117.  
  118.   long reg= 0x7ffff8; /* Initial value of internal register*/
  119.  
  120.   /* Repeat forever */
  121.   for (;;;) {SHIFT-+}
  122.  
  123.     /* Pick out bits to make output value */
  124.     output = (bit(reg,22) << 7) {CBM--}
  125.              (bit(reg,20) << 6) {CBM--}
  126.              (bit(reg,16) << 5) {CBM--}
  127.              (bit(reg,13) << 4) {CBM--}
  128.              (bit(reg,11) << 3) {CBM--}
  129.              (bit(reg, 7) << 2) {CBM--}
  130.              (bit(reg, 4) << 1) {CBM--}
  131.              (bit(reg, 2) << 0);
  132.  
  133.     /* Save bits used to feed bit 0 */
  134.     bit22= bit(reg,22);
  135.     bit17= bit(reg,17);
  136.  
  137.     /* Shift 1 bit left */
  138.     reg= reg << 1;
  139.  
  140.     /(* Feed bit 0 */
  141.     reg= reg {CBM--} (bit22 ^ bit17);
  142.   {SHIFT--}; {SHIFT--};
  143.  
  144. Every loop in the above program provides a new value for the noise waveform in the variable "output".  Notice that the value will repeat for several cycles, just like the real SID.  All in all, I find that the above program can sufficiently reproduce the output from the noise-waveform in the SID.
  145.  
  146. When it comes to the quality of the pseudo-random generated numbers, one thing is clear: Since all bit-patterns of the 23 bits are generated, all values in the 8 bit output will appear equally many times over time.  Furthermore, I believe this particular scheme is recognized as one of the better ones for doing pseudo-random numbers, and it is based on polynomiums, much like the CRCs used for checksumming.
  147.  
  148. But since the values are reproducible, you'll have to be carefull if you plan to use the noise waveform as a source for random numbers for games and such. Firstly, only reset the waveform when the games is loaded.  And wait with the sampling until the user presses a key or fire, which will provide an offset into the stream, so the values will be different from each run.  Furthermore, be sure to read the values at an appropriate sampling-rate, for instance every 32 cycle with a frequency of $8000.  One prefered method is to do a table of say $2000 values before the game starts, so that voice 3 can be used in the game-music or for sound-effects in the game.
  149.  
  150.  
  151. Pulse-waveform --------------
  152.  
  153. At frequency $1000 this program generates the output:
  154.  
  155. for (i=0; i<pulsewidth; i++)
  156.   output= 0;
  157.  
  158. for (;i<$1000;i++)
  159.   output= $ff;
  160.  
  161. The pulsewidth is the value presented in $d410+$d411 for voice 3.  This program should be correct for all values of the pulsewidth, including 0 and $fff.
  162.  
  163. Final words -----------
  164.  
  165. I have studied the other waveforms as well, but I want to get this out now, so the details will wait.  I'd like somebody to verify the data for the noise-waveform, since I have only tried it on my c64 with a 6581 SID produced in week 22 of 1982.  If someone would be a real nice dude, you could try to sample the output (or investigate it with an oscilloscope) from the loud-speaker and determine whether the true output is 8 bit.
  166.  
  167. If interest exists, I'll get myself together and examine the final details of the waveforms and the frequency mapping.  Furthermore, investigation into the envelope could be interesting.  As a final note, I have looked into the mixing of waveforms and some mysterious results have arisen - the results does not suggest a simple AND of the respective waveforms - rather it's strangely organized in bursts every 63 cycle, which suggests correlation with the VIC! I have seen similar correlation in the keyboard reading, which also is clearly associated with the VIC, so this is another topic open for examina- tion.  E-mail me if you want details.
  168.  
  169. -- Asger Alstrup, diku0748@diku.dk
  170.  
  171.  
  172. [Cut here]
  173.  
  174. Programs in assembler ---------------------
  175.  
  176. ;"Cyclewise" - records noise-waveform each cycle using REU. ;----------------------------------------------------------
  177.  
  178.         * = $1000
  179.  
  180. freq    = $8000         ;Define frequency here
  181.  
  182. cycle   jsr init        ;Init screen
  183.  
  184.         lda #$08        ;Set testbit to reset the waveform.
  185.         sta $d412
  186.  
  187.         jsr pause       ;Give it time to settle - nescessary on my machine.
  188.  
  189.         lda #<freq      ;Set frequency
  190.         ldx #>freq
  191.         sta $d40e
  192.         stx $d40f
  193.  
  194.         ;Set up REU
  195.  
  196.         ldx #<$d41b     ;Define REU read address to be $d41b.
  197.         ldy #>$d41b
  198.         stx $df02
  199.         sty $df03
  200.  
  201.         lda #$00        ;Record into $0000
  202.         sta $df04
  203.         sta $df05
  204.         sta $df06       ;in bank 0.
  205.  
  206.         sta $df07       ;Transfer $10000 bytes.
  207.         sta $df08
  208.  
  209.         lda #$00        ;No interrupts from the REU.
  210.         sta $df09
  211.  
  212.         lda #%10000000  ;Fix c64 adress at $d41b.
  213.         sta $df0a
  214.  
  215.         lda #$80
  216.         ldx #%10010000  ;REU command: Do transfer from c64->REU.
  217.  
  218.         ;start waveform and do the sampling
  219.  
  220.         sta $d412       ;Enable noise-waveform
  221.         stx $df01       ;and start recording.
  222.         jmp done        ;After sampling, wrap it up
  223.  
  224.  
  225. ;Loopchecker - checks for a loop in the noise-waveform. ;------------------------------------------------------
  226.  
  227.         * = $1100
  228.  
  229. block   = $2000
  230.  
  231. loop    jsr init        ;Init screen
  232.  
  233.         lda #$08        ;Set testbit to reset the waveform.
  234.         sta $d412
  235.  
  236.         jsr pause       ;Give it time to settle - nescessary on my machine.
  237.  
  238.         lda #$00        ;Set frequency to $8000
  239.         sta $d40e
  240.         lda #$80
  241.         sta $d40f
  242.  
  243.         lda #$80        ;Start noise-waveform
  244.         sta $d412
  245.  
  246.         jsr pause       ;Wait a while, so we get well into the waveform
  247.  
  248.         inc $d020       ;Signal that the recording starts now
  249.  
  250.         ldx #0          ;Sample 256 bytes at 32 cycles l1      lda $d41b
  251.         sta block,x
  252.         bit $ffff
  253.         bit $ffff
  254.         bit $ffff
  255.         bit $ffff
  256.         nop
  257.         inx
  258.         bne l1
  259.  
  260.         nop             ;Wait exactly 1 value
  261.         bit $ea
  262.         bit $ffff
  263.         bit $ffff
  264.  
  265. l2      ldy #$1d        ;See if the 256 bytes repeat
  266.         ldx #$00 l3      lda $d3ff,y     ;(Trick to get 5 cycles execution time)
  267.         bit $ffff
  268.         bit $ffff
  269.         bit $ffff
  270.         bit $ffff
  271.         cmp block,x
  272.         bne l2
  273.         inx
  274.         bne l3
  275.  
  276.         jmp done        ;And exit if they do
  277.  
  278. ;Misc subroutines ;----------------
  279.  
  280.         * = $1800
  281.  
  282.         ;Init screen
  283.  
  284. init    sei             ;Secure accurate timing by
  285.         lda #$00        ;disabling sprites
  286.         sta $d015 in1     lda $d011       ;and screen.
  287.         bpl in1
  288.         and #$ef
  289.         sta $d011
  290.         rts
  291.  
  292.         ;Wait 50 frames so waveform can be reset
  293.  
  294. pause   ldx #50 p1      bit $d011
  295.         bpl p1 p2      bit $d011
  296.         bmi p2
  297.         dex
  298.         bne p1
  299.         rts
  300.  
  301.         ;Wraps it up
  302.  
  303. done    lda $d011       ;Reenable screen
  304.         ora #$10
  305.         sta $d011
  306.  
  307.         cli             ;and exit.
  308.         rts
  309.  
  310. [cut here]
  311.  
  312. Output from "cyclewise" with a frequency of $ffff (The data are correct: the wavelength is effectively 16 cycles for the first $10000 or so values):
  313.  
  314. Value (number of times) $fe ($16), $fc ($30), $f8 ($40), $f0 ($20), $e0 ($30), $c0 ($40), $81 ($20), $03 ($30), $06 ($20), $04 ($20), $0c ($10), $08 ($10), $18 ($30), $30 ($20) ... 
  315.  
  316. -----------------------------
  317.  
  318.  
  319.